<?php
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];
include_once $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$mode = $_GET["mode"];
if($mode == "update"){
	$getListProduct = $managerIUDS->queryData("*", "product", " WHERE product_id = ".$_GET["product_id"], "1", "=");
}else{
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Product Detail :.</title>
<?php
require_once($rootPath . "/" . $contextPath . "/includes/css_inc.php");
require_once($rootPath . "/" . $contextPath . "/includes/js_inc.php");
?>
<script type="text/javascript">
			$(document).ready(function(){ 
				$("#save_data").click(function(){
					var result = true;
					if($("#product_name").val()=="" || $("#product_name").val()==null){
						alert("กรุณากรอกชื่อสินค้า!");
						$("#product_name").focus(); 
						result = false
					}
					if(result && checkfiles()){
						$("#frm1").submit();
					}
				});
				$(function() {
					$( "#combobox" ).combobox();
				});
			}); 
			function checkfiles(){
				var mode = document.getElementById("mode");
				if(mode.value =="update"){
					if(showInput()){
						var fup = document.getElementById('file');
						var fileName = fup.value;
						var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
						if((ext == "jpg" || ext == "JPG") || (ext == "png" || ext == "PNG")){
						return true;
						}else if(fup.value==""){
							alert("กรุณาเลือกรูปที่จะอัพโหลด");
							fup.focus();
							return false;
						}else{
						alert("กรุณาอัพโหลดไฟล์รูปนามสกุล jpg หรือ png");
						fup.focus();
						return false;
						}
					}else{
					return true;
					}
				}else{
					var fup = document.getElementById('file');
					var fileName = fup.value;
					var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
					if((ext == "jpg" || ext == "JPG") || (ext == "png" || ext == "PNG")){
							return true;
						}else if(fup.value==""){
							alert("กรุณาเลือกรูปที่จะอัพโหลด");
							fup.focus();
							return false;
						}else{
							alert("กรุณาอัพโหลดไฟล์รูปนามสกุล jpg หรือ png");
							fup.focus();
							return false;
						}
				}
		}
			function showInput() {
				var rObj1 = document.getElementById("r1");
				var rObj2 = document.getElementById("r2");
				var divObj1 = document.getElementById("div1");
				var divObj2 = document.getElementById("div2");
				if (rObj1.checked) {
					divObj1.style.display = "block";
					divObj2.style.display = "none";
					return false;
				} else if (rObj2.checked) {
					divObj1.style.display = "none";
					divObj2.style.display = "block";
					return true;
				}
			}
		</script>
</head>
<body>
<center>
<form name="frm1" id="frm1" action="../actions/product_action.php"
	method="post" enctype="multipart/form-data">
<table border="0" width=75%">
	<thead class="ui-widget">
		<tr class="ui-widget-header">
			<th colspan="2" align="left">&nbsp;&nbsp;&nbsp;รายละเอียดสินค้า</th>
		</tr>
	</thead>
	<tbody class="ui-widget-content">
		<tr>
			<td align="right">ชื่อสินค้า :</td>
			<td><input type="text" name="product_name" id="product_name"
				value="<?=$getListProduct["product_name"]?>"></td>
		</tr>
		<tr>
			<td align="right">รายละเอียดสินค้า :</td>
			<td><textarea name="product_detail" id="product_detail" cols="21"
				rows="5"><?=$getListProduct["product_detail"]?></textarea></td>
		</tr>
		<? if($mode=="update"){ ?>
		<tr>
			<td align="right">รูปสินค้า :</td>
			<td align="left">
				<table align="left">
				<tr>
					<td align="left">
					<div id="div1" style="display: block;">
						<img src="../productImage/<?=$getListProduct['product_pic_name']?>" height="150" width="150" />
						<input type="hidden" value="<?=$getListProduct['product_pic_name']?>" name="link" id="link" readonly="readonly" />
					</div>
					<div id="div2" style="display: none;">
						<input type="file" name="file" id="file">
					</div>
					</td>
				</tr>
				<tr>
				<td align="left">
					<input type="radio" id="r1" name="chkType" checked="checked" onclick="showInput();" value="L"><label for="r1">ใช้รูปเดิม </label>
					&nbsp;<input type="radio" id="r2" name="chkType" value="U" onclick="showInput();"><label for="r2">ใช้รูปใหม่</label>
				</td>
				</tr>
				</table>
			</td>
		</tr>
		<? } ?>
		<? if($mode=="insert"){ ?>
		<tr>
			<td align="right">Upload ไฟล์</td>
			<td align="left">:&nbsp; <input type="file" name="file" id="file"></td>
		</tr>
		<? } ?>
		<tr>
			<td colspan="2" align="center">
			<input type="button" name="save_data" id="save_data" value="บันทึก">
			<input type="button" value="ยกเลิก" onclick="window.location='product_main.php';"></td>
		</tr>
	</tbody>
</table>
<input type="hidden" id="mode" name="mode" value=<?=$mode?>> <input
	type="hidden" id="product_id" name="product_id"
	value="<?=$_GET["product_id"]?>"></form>
</center>
</body>
</html>
